package com.xhwl.centiotdata.suppliersystem.haikangyunmou;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.constant.HaiKangYunMouConstant;
import com.xhwl.centiotdata.pojo.dto.haikangyunmou.HaiKangYunMouJson;
import com.xhwl.centiotdata.pojo.dto.haikangyunmou.HaiKangYunMouVisitors;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.visitor.VisitorIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.IAuthAccountCentService;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.common.dto.cent.pedes.DeviceHaiKangDTO;
import com.xhwl.common.dto.cent.visitor.VisitorAuthDTO;
import com.xhwl.common.enums.CategoryEnum;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.cent.auth.AuthAccountMapping;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import com.xhwl.common.query.cent.device.ProductAccessQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author: yanyixiong
 * @Description:
 * @Date: create in 15:05 2022/3/4
 */
@Slf4j
@Service
public class VisitorHaiKangYunMouHandler {

    @Autowired
    private HaiKangYunMouUtils haiKangYunMouUtils;
    @Autowired
    private IAuthAccountCentService authAccountCentService;
    @Autowired
    public IDeviceCentService deviceCentService;

    /**
     * 预约授权
     * @param visitors
     * @return
     */
    public String createVisitorAuth(Integer projectId, HaiKangYunMouVisitors visitors) {
        PedestrianIntegrationConfigDTO config = haiKangYunMouUtils.getConfig(projectId);
        return haiKangYunMouUtils.visitors(config, visitors);
    }

    /**
     * 预约授权
     * @param reservationId
     */
    public void deleteVisitorAuth(Integer projectId, String reservationId) {
        PedestrianIntegrationConfigDTO config = haiKangYunMouUtils.getConfig(projectId);
        haiKangYunMouUtils.deleteVisitors(config, reservationId);
    }

    /**
     * 获取人员户室ID
     * @param
     */
    public String getRoomIdByPersonId(Integer projectId, String personId,Integer accountId) {
        List<String> communityId = haiKangYunMouUtils.getCommunityId(projectId,accountId);
        PedestrianIntegrationConfigDTO config = haiKangYunMouUtils.getConfig(projectId);
        log.info("获取的communityId为{}",communityId);
        log.info("获取的open_id为{}",personId);
        List<String> rooms = new ArrayList<>();
        String roomId = null;
        communityId.forEach(id -> {
          String room =   haiKangYunMouUtils.getRoomId(config, personId, id);
          if (Objects.nonNull(room)){
              rooms.add(room);
          }
        });
        if (CollectionUtils.isEmpty(rooms)){
            List<DeviceProjSupplierCategory> deviceConfigList = deviceCentService.listProductAccess((short) 1, new ProductAccessQuery(config.getProjectId(), CategoryEnum.VISITOR_SYSTEM.getId()));
            JSONObject properties = deviceConfigList.get(0).getProperties();
            if (StringUtils.isEmpty(roomId)) {
                roomId = properties.getString("defaultRoomId");
                log.error("获取的roomId为{}",roomId);
                // throw new BusinessException("海康云眸获取人员户室信息失败");
            }
            log.error("获取的roomId为{}",roomId);
        }else {
            roomId = rooms.get(0);
        }
        return roomId;

    }

    /**
     * 根据中台人员id 获取海康云眸人员id
     * @param dto
     * @return
     */
    private String getPersonId(VisitorAuthDTO dto) {
        AuthAccountMapping authAccountMapping = authAccountCentService.query(dto.getProjectId(),
                dto.getIntervieweeId(), dto.getProductId());
        if (Objects.isNull(authAccountMapping)|| StringUtils.isEmpty(authAccountMapping.getOpenCode())) {
            throw new BusinessException("中台用户与海康云眸用户映射异常");
        }
        return authAccountMapping.getOpenId();
    }

    /**
     * 海康云眸获取人员门禁设备信息
     * @param
     * @return
     */
    public List<DeviceHaiKangDTO> getAuthDeviceList(String personId, String communityId, VisitorIntegrationConfigDTO config){
        HaiKangYunMouJson resp = haiKangYunMouUtils.getAuthDevices(personId,communityId,config);
        log.info("海康人员设备信息{}",resp.getData());
        List<DeviceHaiKangDTO> deviceHaiKangDTOS;
        if (resp.getCode().equals(HaiKangYunMouConstant.SUCCESS_CODE)){
            deviceHaiKangDTOS = JSONObject.parseArray(JSONObject.toJSONString(resp.getData()), DeviceHaiKangDTO.class);
            log.info("deviceHaiKangDTOS{}",deviceHaiKangDTOS);
            return deviceHaiKangDTOS;
        }else {
            return null;
        }
    }

}
